//
//  MineViewController.m
//  copyrightTrading
//
//  Created by 郭子一 on 2020/10/5.
//  Copyright © 2020 郭子一. All rights reserved.
//

#import "MineViewController.h"
#import "MineCell.h"
#import "MineBuyViewController.h"
#import "MineCartViewController.h"
#import "MineWalletViewController.h"
#import "MineMessageViewController.h"
#import "MineDownloadViewController.h"
#import "MineSettingViewController.h"
#import "MineInfoViewController.h"
#import "MineBrowseRecordsViewController.h"
@interface MineViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UIImageView *avatorImg;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *identityLabel;
@property (nonatomic, strong) UIButton *loginBtn;
@property (nonatomic, strong) JSBadgeView *cartNumView;  // 购物车数量

@property (nonatomic, strong) NSArray *imgArray;
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) NSDictionary *userInfo; // 用户信息
@property (nonatomic, strong) NSArray *districtList; // 用户地区
@property (nonatomic, strong) NSArray *indsutryList; // //用户行业

@end

@implementation MineViewController

- (NSDictionary *)userInfo
{
    if (!_userInfo) {
        _userInfo = [NSDictionary dictionary];
    }
    return _userInfo;
}

- (NSArray *)districtList
{
    if (!_districtList) {
        _districtList = [NSArray array];
    }
    return _districtList;
}

- (NSArray *)indsutryList
{
    if (!_indsutryList) {
        _indsutryList = [NSArray array];
    }
    return _indsutryList;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[APIManager sharedNetManager] requestWithMethod:HttpRequestTypeGet URLString:@"user" parameters:@{} done:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        NSDictionary *dataDic = responseObject[@"data"];
        
        self.userInfo = dataDic[@"user_info"];
        self.indsutryList = dataDic[@"indsutry_list"];
        self.districtList = dataDic[@"district_list"];
        
        [self.avatorImg m_setImageWithUrlString:self.userInfo[@"head_img"] placeholder:@"MineHeaderImg"];
        self.nameLabel.text = self.userInfo[@"nickname"];
        self.identityLabel.text = [NSString stringWithFormat:@"%@|%@",self.userInfo[@"district_name"],self.userInfo[@"industry_name"]];
        self.cartNumView.badgeText = [NSString stringWithFormat:@"%@",self.userInfo[@"shopping_count"]];
        
        self.loginBtn.hidden = [DVUserManager isVisitor] ? NO:YES;
        self.nameLabel.hidden = [DVUserManager isVisitor] ? YES:NO;
        self.identityLabel.hidden = [DVUserManager isVisitor] ? YES:NO;
        
        [self.tableView reloadData];
    } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        self.loginBtn.hidden = [DVUserManager isVisitor] ? NO:YES;
        self.nameLabel.hidden = [DVUserManager isVisitor] ? YES:NO;
        self.identityLabel.hidden = [DVUserManager isVisitor] ? YES:NO;
        self.avatorImg.image = [UIImage imageNamed:@"MineHeaderImg"];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleArray = @[@"我的购买",@"我的钱包",@"我的下载",@"浏览记录",@"我的消息"];
    self.imgArray = @[@"MineBuy",@"MinePacket",@"MineDownload",@"MineSafari",@"MineNoti"];
}

- (void)setupUI
{
    self.view.backgroundColor = RGB_VALUE(0xF5F5F5);
    
    UIImageView *headerImg = [[UIImageView alloc] init];
    headerImg.backgroundColor = UIColor.redColor;
    headerImg.userInteractionEnabled = YES;
    [self.view addSubview:headerImg];
    [headerImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.offset(0);
        make.height.mas_equalTo(268);
    }];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = @"我的";
    titleLabel.font = JSS_MEDIUM_FONT(17);
    titleLabel.textColor = RGB_VALUE(0xFFFFFF);
    [headerImg addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(11+Height_StatusBar);
        make.centerX.offset(0);
        make.width.mas_equalTo(35);
        make.height.mas_equalTo(22);
    }];
    
    UIButton *setBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [setBtn setImage:[UIImage imageNamed:@"MineSetting"] forState:UIControlStateNormal];
    [setBtn addTarget:self action:@selector(setBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [setBtn setHitEdgeInsets:UIEdgeInsetsMake(-15, -15, -15, -15)];
    [headerImg addSubview:setBtn];
    [setBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(titleLabel);
        make.left.offset(20);
        make.height.width.mas_equalTo(21);
    }];
    
    UIButton *cartBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cartBtn setImage:[UIImage imageNamed:@"MineCart"] forState:UIControlStateNormal];
    [cartBtn addTarget:self action:@selector(cartBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [headerImg addSubview:cartBtn];
    [cartBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(titleLabel);
        make.right.offset(-20);
        make.height.width.mas_equalTo(21);
    }];
    
    self.cartNumView = [[JSBadgeView alloc] initWithParentView:cartBtn alignment:JSBadgeViewAlignmentTopRight];
    self.cartNumView.badgeTextFont = JSS_MEDIUM_FONT(9);
    self.cartNumView.badgeText = @"0";
    self.cartNumView.badgeBackgroundColor = RGB_VALUE(0xFF9417);
    self.cartNumView.badgeStrokeColor = RGB_VALUE(0xFF9417);
    //当更新数字时，最好刷新，不然由于frame固定的，数字为2位时，红圈变形
//    [self.badgeView setNeedsLayout];
    
//    MineHeaderImg
    self.avatorImg = [[UIImageView alloc] init];
    self.avatorImg.image = [UIImage imageNamed:@"MineHeaderImg"];
    self.avatorImg.userInteractionEnabled = YES;
    self.avatorImg.layer.masksToBounds = YES;
    self.avatorImg.layer.cornerRadius = 48;
    [headerImg addSubview:self.avatorImg];
    [self.avatorImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom).offset(31);
        make.centerX.offset(0);
        make.height.width.mas_equalTo(96);
    }];
    
    UITapGestureRecognizer *tapAvator = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAvator)];
    [self.avatorImg addGestureRecognizer:tapAvator];
    
    self.loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.loginBtn setTitle:@"登录/注册" forState:UIControlStateNormal];
    [self.loginBtn setTitleColor:RGB_VALUE(0xFFFFFF) forState:UIControlStateNormal];
    [self.loginBtn.titleLabel setFont:JSS_MEDIUM_FONT(24)];
    [self.loginBtn addTarget:self action:@selector(loginBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [headerImg addSubview:self.loginBtn];
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.avatorImg.mas_bottom).offset(11);
        make.centerX.offset(0);
        make.width.mas_equalTo(115);
        make.height.mas_equalTo(33);
    }];
    
    self.nameLabel = [UILabel new];
    self.nameLabel.font = JSS_MEDIUM_FONT(17);
    self.nameLabel.textColor = RGB_VALUE(0xFFFFFF);
    self.nameLabel.textAlignment = NSTextAlignmentCenter;
    [headerImg addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.avatorImg.mas_bottom).offset(5);
        make.centerX.offset(0);
        make.left.right.offset(0);
        make.height.mas_equalTo(24);
    }];

    self.identityLabel = [UILabel new];
    self.identityLabel.font = JSS_MEDIUM_FONT(12);
    self.identityLabel.textColor = RGB_VALUE(0xFFFFFF);
    self.identityLabel.textAlignment = NSTextAlignmentCenter;
    [headerImg addSubview:self.identityLabel];
    [self.identityLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLabel.mas_bottom).offset(0);
        make.centerX.offset(0);
        make.left.right.offset(0);
        make.height.mas_equalTo(17);
    }];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor = UIColor.clearColor;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerClass:[MineCell class] forCellReuseIdentifier:@"MineCell"];
    [self.view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(headerImg.mas_bottom).offset(10);
        make.height.mas_equalTo(220);
    }];
}

- (void)setBtnClick:(UIButton *)sender
{
    if ([DVUserManager isVisitor]) {
        LoginViewController *vc = [[LoginViewController alloc] init];
        [self pushVc:vc];
        return;
    }
    MineSettingViewController *vc = [[MineSettingViewController alloc] init];
    [self pushVc:vc];
}

- (void)cartBtnClick:(UIButton *)sender
{
    if ([DVUserManager isVisitor]) {
        LoginViewController *vc = [[LoginViewController alloc] init];
        [self pushVc:vc];
        return;
    }
    MineCartViewController *vc = [[MineCartViewController alloc] init];
    [self pushVc:vc];
}

// 点击头像
- (void)tapAvator
{
    if ([DVUserManager isVisitor]) {
        LoginViewController *vc = [[LoginViewController alloc] init];
        [self pushVc:vc];
        return;
    }
    MineInfoViewController *vc = [[MineInfoViewController alloc] init];
    vc.userInfo = self.userInfo;
    vc.indsutryList = self.indsutryList;
    vc.districtList = self.districtList;
    [self pushVc:vc];
}

- (void)loginBtnClick:(UIButton *)sender
{
    LoginViewController *vc = [[LoginViewController alloc] init];
    [self pushVc:vc];
//    DVUserManager *manager = [DVUserManager shareManager];
//    [manager setRootViewController:manager.loginController];
}

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MineCell"];
    cell.leftImg.image = [UIImage imageNamed:self.imgArray[indexPath.row]];
    cell.titleLab.text = self.titleArray[indexPath.row];
    if (indexPath.row == 4) {
        cell.subLab.text = [NSString stringWithFormat:@"%@",NULLString(self.userInfo[@"message_count"])?@"0":self.userInfo[@"message_count"]];
        cell.subLab.hidden = NO;
    } else {
        cell.subLab.text = @"";
        cell.subLab.hidden = YES;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([DVUserManager isVisitor]) {
        LoginViewController *vc = [[LoginViewController alloc] init];
        [self pushVc:vc];
        return;
    }
    if (indexPath.row == 0) {
        MineBuyViewController *vc = [[MineBuyViewController alloc] init];
        [self pushVc:vc];
    } else if (indexPath.row == 1) {
        MineWalletViewController *vc = [[MineWalletViewController alloc] init];
        [self pushVc:vc];
    } else if (indexPath.row == 2) {
        MineDownloadViewController *vc = [[MineDownloadViewController alloc] init];
        [self pushVc:vc];
    } else if (indexPath.row == 4) {
        MineMessageViewController *vc = [[MineMessageViewController alloc] init];
        [self pushVc:vc];
    } else if (indexPath.row == 3) {
        MineBrowseRecordsViewController *vc = [[MineBrowseRecordsViewController alloc] init];
        [self pushVc:vc];
    }
    
    
}


@end
